<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="articleTitle">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入文章标题"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="评论状态" clearable style="width: 240px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:comment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          :disabled="multiple"
          @click="handleApprove"
          v-hasPermi="['system:comment:approve']"
        >审核通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          :disabled="multiple"
          @click="handleReject"
          v-hasPermi="['system:comment:reject']"
        >驳回</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="commentId" width="80" />
      <el-table-column label="文章标题" align="center" prop="articleTitle" :show-overflow-tooltip="true" />
      <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="评论人" align="center" prop="userName" width="100" />
      <el-table-column label="点赞数" align="center" prop="likeCount" width="80" />
      <el-table-column label="回复数" align="center" prop="replyCount" width="80" />
      <el-table-column label="IP" align="center" prop="ip" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">已通过</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="danger">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评论时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:comment:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            v-if="scope.row.status === '0'"
            v-hasPermi="['system:comment:approve']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleReject(scope.row)"
            v-if="scope.row.status === '0'"
            v-hasPermi="['system:comment:reject']"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:comment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 评论详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="文章标题：">
          <span>{{ form.articleTitle }}</span>
        </el-form-item>
        <el-form-item label="评论人：">
          <span>{{ form.userName }}</span>
        </el-form-item>
        <el-form-item label="评论内容：">
          <span>{{ form.content }}</span>
        </el-form-item>
        <el-form-item label="评论时间：">
          <span>{{ form.createTime }}</span>
        </el-form-item>
        <el-form-item label="审核意见：" v-if="form.status !== '0'">
          <span>{{ form.reviewRemark || '无' }}</span>
        </el-form-item>
        <el-form-item label="审核意见：" v-if="form.status === '0'">
          <el-input v-model="form.reviewRemark" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm" v-if="form.status === '0'">审核通过</el-button>
        <el-button type="warning" @click="rejectForm" v-if="form.status === '0'">驳 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComment, getComment, delComment, approveComment, rejectComment } from "@/api/comment";

export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态选项
      statusOptions: [
        { value: "0", label: "待审核" },
        { value: "1", label: "已通过" },
        { value: "2", label: "已驳回" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleTitle: undefined,
        userName: undefined,
        status: undefined
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commentId: undefined,
        articleId: undefined,
        articleTitle: undefined,
        userId: undefined,
        userName: undefined,
        content: undefined,
        status: undefined,
        reviewRemark: undefined
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const commentId = row.commentId || this.ids[0];
      getComment(commentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "评论详情";
      });
    },
    /** 审核通过按钮操作 */
    handleApprove(row) {
      this.reset();
      const commentId = row.commentId || this.ids[0];
      getComment(commentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核评论";
      });
    },
    /** 驳回按钮操作 */
    handleReject(row) {
      this.reset();
      const commentId = row.commentId || this.ids[0];
      getComment(commentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "驳回评论";
      });
    },
    /** 提交审核通过按钮 */
    submitForm() {
      if (!this.form.reviewRemark) {
        this.form.reviewRemark = "内容符合规范，审核通过";
      }
      approveComment(this.form.commentId, this.form.reviewRemark).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交驳回按钮 */
    rejectForm() {
      if (!this.form.reviewRemark) {
        this.$modal.msgError("请输入驳回理由");
        return;
      }
      rejectComment(this.form.commentId, this.form.reviewRemark).then(response => {
        this.$modal.msgSuccess("驳回成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal.confirm('是否确认删除评论编号为"' + commentIds + '"的数据项？').then(function() {
        return delComment(commentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script> 